/*

* Created on Aug 4, 2006

*/

package com.arch.dev.client.view.ui;

 

import com.google.gwt.user.client.ui.AbstractImagePrototype;

import com.google.gwt.user.client.ui.Button;

 

 

/**

* Create a button that has an image and text to the right hand side.

*

 * <p>

* <img src='/docmgr/javadocs/components/snapshots/ImageButton.png'>

* </p>

*

 * <p>

* <b>CSS Style Rules</b> <blockquote> .nsp-ImageButton { } </blockquote>

* <p>

* <b>Code Sample:</b> <br/> <blockquote style='background-color: #DDDDDD;'>

*

 * <pre>

* VerticalPanel vp = new VerticalPanel();

* vp.add(new Label(&quot;ImageButton&quot;));

* vp.add(new ImageButton(&quot;Hello&quot;, +&quot;Icons.SUBMIT&quot;));

* </pre>

*

 * </blockquote>

*

 *

 */

public class ImageButton extends Button {

 

    private String text;

 

    private String imageUrl;

   

    private String tooltip;

   

    private AbstractImagePrototype image = null;

 

    /**

     * Default Constructor.

     *

     */

    public ImageButton() {

        super();

        setStyleName("nsp-ImageButton");

    }

 

    /**

     * Constructor with the text and the image specified as parameters.

     *

     * @param text

     *            Text of the button

     * @param imageUrl

     *            URL of the image. Either a fully qualified URL must be

     *            specified or an path must be specified.

     */

    public ImageButton(String text, String imageUrl) {

        setStyleName("nsp-ImageButton");

        setTextAndImage(text, imageUrl);

    }

   

  

    public ImageButton(String text, String imageUrl, String tooltip) {

        setStyleName("nsp-ImageButton");

        setTextAndImage(text, imageUrl);

        setTooltip(tooltip);

    }

   

    

    

    /**

     * Constructor that takes text, that will be displayed and image that should be shown

     * @param text - text do display (use null if nothing shall be displayed)

     * @param image - GWT image probe (i.e. from IconsImageBundle)

     */

    public ImageButton(String text, AbstractImagePrototype image) {

        setStyleName("nsp-ImageButton");

        setTextAndImage(text, image);

    }

   

 

    public ImageButton(String text, AbstractImagePrototype image, String tooltip) {

        setStyleName("nsp-ImageButton");

        setTextAndImage(text, image);

        setTooltip(tooltip);

    }

   

 

    /**

     * Sets the image and text for the button. Can be used be anytime to change

     * the image and text for the button.

     *

     * @param text

     *            Text of the button

     * @param imageUrl

     *            URL of the image. Either a fully qualified URL must be

     *            specified or an path must be specified.

     */

    public void setTextAndImage(String text, String imageUrl) {

        this.text = text;

        this.imageUrl = imageUrl;

 

        drawButton();

    }

 

    /**

     * Sets the image and text for the button. Can be used be anytime to change

     * the image and text for the button.

     *

     * @param text

     *            Text of the button

     * @param image

     *            GWT image probe from image bundle

     */

    public void setTextAndImage(String text, AbstractImagePrototype image) {

        this.text = text;

        this.image = image;

 

        drawButton();

    }

   

    

    

    private void drawButton() {

        String html = "";

        if (image != null) {

            html = image.getHTML();

            /**

             * this fix was due to error in IE6 and GWT.

             * Later the usage of image bundle was disabled for IE.

             * Once the issue will be fixed, this code may be in some fomr reused

            if (GWTUtils.isIE()) {

                // Due to bug in IE6 - we must modify AlphaImageLoader filter and add alpha filter.

                // If we combined stand-alone AlphaImageLoader applied on image element

                // with another filter (such as gray when disabling the button) on parent element,

                // IE6 browser would crash immediately. When combining with alpha filter, it is OK.

                html = html.replaceAll("'crop'\\)", "'crop') alpha(opacity=100)");

               

                // Due to another bug in IE6 - we must put transparent div before image loaded by

                // AlphaImageLoader filter. Otherwise hitting the button right above the image

                // would not redraw button to look like being pressed.

                Image img = image.createImage();

                html = "<div style='position:absolute; background-color:opaque; width:" + img.getWidth() + "px; height:" + img.getHeight() + "px;'></div>" + html;

            }

            */

        } else if (imageUrl != null)

        {

           

                html = "<img border='0' align='middle' src='" + imageUrl + "'/>";

        } else

        {

                html = "<img border='0' align='middle' src=''/>";

        }

        if (text != null && !text.equals("")) {

            html += "&nbsp;" + text;

        }

        setHTML(html);

       

        

        if (tooltip != null && !tooltip.equals("")) {

            setTitle(tooltip);

        }

       

    }

 

    /**

     * @return Returns the imageUrl.

     */

    public String getImageUrl() {

        return imageUrl;

    }

 

    /**

     * Sets a image for the button. Can be used be anytime to change the image

     * for the button.

     *

     * @param imageUrl

     *            URL of the image. Either a fully qualified URL must be

     *            specified or an path must be specified.

     */

    public void setImageUrl(String imageUrl) {

        this.imageUrl = imageUrl;

        drawButton();

    }

 

    /**

     * Sets a image for the button. Can be used be anytime to change the image

     * for the button.

     *

     * @param image

     *            GWT image probe from image bundle           

     */

    public void setImage(AbstractImagePrototype image) {

        this.image = image;

        drawButton();

    }

   

    /**

     * @return Returns the text.

     */

    public String getText() {

        return text;

    }

 

    /**

     * Sets a text for the button. Can be used be anytime to change the text of

     * the button.

     *

     * @param text

     *            Text of the button

     */

    public void setText(String text) {

        this.text = text;

        drawButton();

    }

 

   

    public void setTooltip(String tooltip) {

        this.tooltip = tooltip;

        drawButton();

    }

   

    public void setEnabled(boolean enabled){

      if (enabled){

            setStyleName("nsp-ImageButton");

      }

      else {

            setStyleName("nsp-ImageButton-disabled");

      }

      super.setEnabled(enabled);

    }

}